﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using KimPhongCRM.Models;
using DevExpress.Web.Mvc;

namespace KimPhongCRM.Controllers
{
    [PermissionFilter]
    public class ConfigController : Controller
    {
        private Entities db = new Entities();

        //
        // GET: /Config/

        public ViewResult Index()
        {
            return View(db.tblEmailConfigs.ToList());
        }
        public ActionResult Email()
        {
            var model = db.tblEmailConfigs.ToList();
            return View(db.tblEmailConfigs.ToList());
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
        KimPhongCRM.Models.Entities db1 = new KimPhongCRM.Models.Entities();

        [ValidateInput(false)]
        public ActionResult listEmailConfigPartial()
        {
            var model = db1.tblEmailConfigs;
            return PartialView("_listEmailConfigPartial", model.ToList());
        }

        [HttpPost, ValidateInput(false)]
        public ActionResult listEmailConfigPartialAddNew(KimPhongCRM.Models.tblEmailConfig item)
        {
            var model = db1.tblEmailConfigs;
            if (ModelState.IsValid)
            {
                try
                {
                    model.Add(item);
                    db1.SaveChanges();
                }
                catch (Exception e)
                {
                    ViewData["EditError"] = e.Message;
                }
            }
            else
                ViewData["EditError"] = "Please, correct all errors.";
            return PartialView("_listEmailConfigPartial", model.ToList());
        }
        [HttpPost, ValidateInput(false)]
        public ActionResult listEmailConfigPartialUpdate(KimPhongCRM.Models.tblEmailConfig item)
        {
            var model = db1.tblEmailConfigs;
            if (ModelState.IsValid)
            {
                try
                {
                    var modelItem = model.FirstOrDefault(it => it.ecid == item.ecid);
                    if (modelItem != null)
                    {
                        this.UpdateModel(modelItem);
                        db1.SaveChanges();
                    }
                }
                catch (Exception e)
                {
                    ViewData["EditError"] = e.Message;
                }
            }
            else
                ViewData["EditError"] = "Please, correct all errors.";
            return PartialView("_listEmailConfigPartial", model.ToList());
        }
        [HttpPost, ValidateInput(false)]
        public ActionResult listEmailConfigPartialDelete(System.Int64 ecid)
        {
            var model = db1.tblEmailConfigs;
            if (ecid != null)
            {
                try
                {
                    var item = model.FirstOrDefault(it => it.ecid == ecid);
                    if (item != null)
                        model.Remove(item);
                    db1.SaveChanges();
                }
                catch (Exception e)
                {
                    ViewData["EditError"] = e.Message;
                }
            }
            return PartialView("_listEmailConfigPartial", model.ToList());
        }
    }
}